global SSDIMed "/disk/agedisk4/medicare.work/miller-DUA50377/proj_ssdi"
* Settings
version 16
do "$SSDIMed/scripts/_auxiliary/_project_settings.do"

* Set graph style to project default settings
*	Pass as arguments: height width (default 3.5in 6.5in)
*do "$SSDIMed/scripts/_auxiliary/_project_grstyle.do" 3.1in 6.5in
capture log close 
log using $SSDIMed/scripts/logs/06_afig5_MedicaidMedAdv.log, text replace 
 


* Force redo of estimation?
*   redo_estimates = 0: use existing estimates, if available
*   redo_estimates = 1: redo estimation, even if existing estimates are available
local redo_estimates 0

* -------------------------------------------------------------------------------------------------
* Computation DONE IN 04_cohort_regressions.do
* -------------------------------------------------------------------------------------------------
**get yearly unemployment 
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear
gcollapse (mean) nat_unemp_mean=unemp_rate_national_atapp   [aw = pop_age_atapp], by(covstart_year) fast 
save "$SSDIMed/data/proc/public/unempatapp_covstartyear.dta", replace 

* DI entrant cohort average Medicare spending
foreach y in dual hmo partb{
local clust county_mofd
local x covstart_year
local sample 2262  
  * Control variables
  local ctrl 01
  
  * x range (depends on sample)
  assert "`sample'" == "2262"
  local covstart_year_min 1993
  local covstart_year_max 2016
  local entry_age_min 22
  local entry_age_max 62
  
  * Load estimates
estimates use "$SSDIMed/results/estimates/x-i.`x'/x-i.`x'_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'.ster"
forvalues val = ``x'_min'/``x'_max' {
if `val'==``x'_min' qui lincom _cons
else qui lincom _cons+`val'.covstart_year 
local coef`val'=`r(estimate)'
local lb`val'=`r(lb)'
local ub`val'=`r(ub)'
}

clear 
set obs 25 
gen covstart_year=_n+1992
gen coef=. 
gen lb=.
gen ub=.
forvalues val = ``x'_min'/``x'_max' {
qui replace coef=`coef`val'' if covstart_year==`val'
qui replace lb=`lb`val'' if covstart_year==`val'
qui replace ub=`ub`val'' if covstart_year==`val'
}
***get unemployment 
merge 1:1 covstart_year using "$SSDIMed/data/proc/public/unempatapp_covstartyear.dta", keep(1 3)  nogen 
replace covstart_year=covstart_year-2
if "`y'"=="partb" local ylabel "ylabel(.89(.03).98, axis(1))"
if "`y'"=="hmo" local ylabel "ylabel(.1(.1).4, axis(1))"
if "`y'"=="dual" local ylabel "ylabel(.2(.2).8, axis(1))"
local cis "(rarea ub lb covstart_year, yaxis(1) color(gs10)) "
local UR "(line nat_unemp_mean covstart_year, yaxis(2) lpat(dash) lwid(thick) color("126 153 180"))"
local p "(line coef covstart_year, yaxis(1) lwid(thick) color(black) )"
twoway `cis' `UR' `p' , legend(off)  ylabel(4(2)10, axis(2)) `ylabel' xlabel(1990(4)2014) ytitle("Cohort Average Enrollment (BLACK)", axis(1)) ytitle("Nat'l Unemp Rate at App (BLUE)", axis(2))  xtitle("Year of Entry")  graphregion(color(white))  bgcolor(white) 
graph export $SSDIMed/results/figures/entry_year_cohort_`y'.pdf, as(pdf) replace
*graph display, ysize(3) xsize(6)
*graph export $SSDIMed/results/figures/entry_year_cohort_`y'.pdf, as(pdf) replace
}



***ANOTHER VERSION : dual/hmo/partb by ventile of unemployment  
* DI entrant cohort average Medicare spending
foreach y in dual hmo partb{
local clust county_mofd
local x unemp_rate_q20_atapp
local sample 2262  
  * Control variables
  local ctrl 02
  
  * x range (depends on sample)
  assert "`sample'" == "2262"
  local covstart_year_min 1993
  local covstart_year_max 2016
  local unemp_rate_q20_atapp_min 1
  local unemp_rate_q20_atapp_max 20
  
  * Load estimates
estimates use "$SSDIMed/results/estimates/x-i.`x'/x-i.`x'_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'.ster"
forvalues val = ``x'_min'/``x'_max' {
if `val'==``x'_min' qui lincom _cons
else qui lincom _cons+`val'.`x' 
local coef`val'=`r(estimate)'
local lb`val'=`r(lb)'
local ub`val'=`r(ub)'
}

clear 
set obs 20
gen ventile=_n 
gen coef=. 
gen lb=.
gen ub=.
forvalues val = ``x'_min'/``x'_max' {
qui replace coef=`coef`val'' if ventile==`val'
qui replace lb=`lb`val'' if ventile==`val'
qui replace ub=`ub`val'' if ventile==`val'
}
***get unemployment 
*merge 1:1 covstart_year using "$SSDIMed/data/proc/public/unempatapp_covstartyear.dta", keep(1 3)  nogen 
*replace covstart_year=covstart_year-2
if "`y'"=="partb" local ylabel "ylabel(.89(.03).98, axis(1))"
if "`y'"=="hmo" local ylabel "ylabel(.1(.1).4, axis(1))"
if "`y'"=="dual" local ylabel "ylabel(.2(.2).8, axis(1))"
local cis "(rarea ub lb ventile, yaxis(1) color(gs10)) "
*local UR "(line nat_unemp_mean ventile, yaxis(2) lpat(dash) lwid(thick) color("126 153 180"))"
local p "(line coef ventile, yaxis(1) lwid(thick) color(black) )"
twoway `cis' `p' , legend(off)  `ylabel' ytitle("Cohort Average Enrollment (BLACK)", axis(1))  xtitle("Ventile")  graphregion(color(white))  bgcolor(white) 
graph export $SSDIMed/results/figures/unemp_ventile_`y'.pdf, as(pdf) replace
*graph display, ysize(3) xsize(6)
*graph export $SSDIMed/results/figures/entry_year_cohort_`y'.pdf, as(pdf) replace
}




foreach y in dual hmo partb{
local clust county_mofd
local x age_year_covstart_fill
local sample main  
  * Control variables
  local ctrl 02
  local entry_year_min 1991
  local entry_year_max 2014
  local age_year_covstart_fill_min 22
  local age_year_covstart_fill_max 62
estimates use "$SSDIMed/results/estimates/x-i.`x'/x-i.`x'_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'.ster"
forvalues val = ``x'_min'/``x'_max' {
if `val'==``x'_min' qui lincom _cons
else qui lincom _cons+`val'.age_year_covstart_fill 
local coef`val'=`r(estimate)'
local lb`val'=`r(lb)'
local ub`val'=`r(ub)'
}

clear 
set obs 43 
gen `x'=_n+19
gen coef=. 
gen lb=.
gen ub=.
forvalues val = ``x'_min'/``x'_max' {
qui replace coef=`coef`val'' if `x'==`val'
qui replace lb=`lb`val'' if `x'==`val'
qui replace ub=`ub`val'' if `x'==`val'
}
replace `x'=`x'-2 
if "`y'"=="partb" local ylabel "ylabel(.89(.03).98, axis(1))"
if "`y'"=="hmo" local ylabel "ylabel(.1(.1).4, axis(1))"
if "`y'"=="dual" local ylabel "ylabel(.2(.2).8, axis(1))"
local cis "(rarea ub lb `x', yaxis(1) color(gs10)) "
local p "(line coef `x', yaxis(1) lwid(thick) color(black) )"
twoway  `cis' `p', legend(off) `ylabel' xlabel(20(5)60) ytitle("Cohort Average Enrollment", axis(1))  xtitle("Age at Entry")  graphregion(color(white))  bgcolor(white) xline(50, lcol(black) lpat(dash)) xline(55, lcol(black) lpat(dash))
graph export $SSDIMed/results/figures/entry_age_cohort_`y'.pdf, as(pdf) replace
*graph display, ysize(3) xsize(6)
*graph export $SSDIMed/results/figures/entry_age_cohort_`y'.pdf, as(pdf) replace
}


